package com.aiyan.edu.service.search.controller;

import com.aiyan.edu.api.search.CourseEsSearchControllerApi;
import com.aiyan.edu.framework.domain.course.CoursePlanMediaPub;
import com.aiyan.edu.framework.domain.course.CoursePub;
import com.aiyan.edu.framework.domain.search.CourseSearchParam;
import com.aiyan.edu.framework.model.response.CommonCode;
import com.aiyan.edu.framework.model.response.CommonResponseResult;
import com.aiyan.edu.framework.model.response.QueryResponseResult;
import com.aiyan.edu.framework.web.BaseController;
import com.aiyan.edu.service.search.service.CourseEsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: zqzhou
 * @create: 2019/11/13 11:50
 **/
@RestController
@RequestMapping("/search/course")
public class CourseEsSearchController extends BaseController implements CourseEsSearchControllerApi {

    @Autowired
    CourseEsService courseEsService;

    /**
     * 课程搜索
     *
     * @param page
     * @param size
     * @param courseSearchParam
     * @return
     */
    @Override
    @GetMapping("/list/{page}/{size}")
    public QueryResponseResult<CoursePub> findList(@PathVariable("page") int page, @PathVariable("size") int size, CourseSearchParam courseSearchParam) {
        return this.courseEsService.findList(page, size, courseSearchParam);
    }

    @Override
    @GetMapping("/{id}")
    public CommonResponseResult<CoursePub> find(@PathVariable("id") String id) {
        return this.courseEsService.find(id);
    }

    @Override
    @GetMapping("/media/{coursePlanId}")
    public CommonResponseResult<CoursePlanMediaPub> findCourseMedia(@PathVariable("coursePlanId") String coursePlanId) {
        String[] ids = new String[]{coursePlanId};
        QueryResponseResult queryResponseResult = this.courseEsService.findCourseMedia(ids);
        if (!CollectionUtils.isEmpty(queryResponseResult.getQueryResult().getList())) {
            return new CommonResponseResult<>(CommonCode.SUCCESS, (CoursePlanMediaPub) queryResponseResult.getQueryResult().getList().get(0));
        }

        return new CommonResponseResult<>(CommonCode.SUCCESS, null);
    }
}
